﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Sparkle.Hosting
{
    /// <summary>
    /// Interaction logic for UserControlLinks.xaml
    /// </summary>
    public abstract partial class LinkPanel : UserControl, ILinkPanel
    {
        public LinkPanel()
        {
            InitializeComponent();
        }

        #region ILinkPanel Members

        public string Title { get { return this.txtTitle.Text; } set { this.txtTitle.Text = value; } }

        public UIElementCollection Links { get { return stkLinks.Children; } }

        public void PanelChanged(UserControl newObject)
        {
            if (newObject is UserControl)
            {
                OnControlChange((newObject as UserControl), (newObject as UserControl).DataContext);
            }
        }        

        public event LinkHandler OnLinkClick;

        public event LinkHandler OnControlChange;

        #endregion
        
        public virtual   void LinkButton_Click(object sender, RoutedEventArgs e)
        {
            if(OnLinkClick!=null)
            {
                OnLinkClick(sender, (sender is HyperlinkButton)  ? (sender as HyperlinkButton).Tag: null);
            }
        }

        #region Statics methods

        public static void ExtractLinks<T>(object linkContainer, UIElementCollection linkCollection, RoutedEventHandler onClick)
            where T:Attribute
        {
            if (linkContainer == null)
                return;
            if (linkCollection == null)
                return;
            
            linkCollection.Clear();

            object [] linksAttrib = linkContainer.GetType().GetCustomAttributes(typeof(T), true);

            if (linksAttrib != null)
            {
                foreach (var item in linksAttrib)
                {
                    if (item is IDynamicLink)
                    {
                        IDynamicLink dynamicLink= item as IDynamicLink;

                        IList<object> dyLinks = dynamicLink.GetDynamicLinks(linkContainer); 
                        if (dyLinks != null)
                        {
                            foreach (var dlink in dyLinks)
                            {
                                HyperlinkButton hLinkButton = new HyperlinkButton() { Content = dlink.ToString(), Tag = dlink };
                                hLinkButton.Click += onClick;
                                linkCollection.Add(hLinkButton);  
                            }
                        }
                    }
                    else
                    {
                        HyperlinkButton hLinkButton = new HyperlinkButton() { Content = item.ToString(), Tag = item };
                        hLinkButton.Click += onClick;
                        linkCollection.Add(hLinkButton);
                    }
                }
            }
        }

        #endregion
    }
}
